




public class Szimulacio extends Thread {


	public int N,R,S,T,P,elinditva;
	public int[][] a;
	private int[][] b;
	private int[][] temp;
	private MatrixGenerator m;
	private MyCanvas canvas;
	public void setN(int n)
	{
		this.N = n;
	}
	public void setR(int r)
	{
		this.R = r;
	}
	public void setS(int s)
	{
		this.S = s;
	}
	public void setT(int t)
	{
		this.T = t;
	}
	public void setP(int p)
	{
		this.P = p;
	}
	public void setCanvas(MyCanvas canvas)
	{
		this.canvas = canvas;
	}
	
	public void init()
	{
		a = new int [ N ] [ N ]; //strategia
		b = new int [ N ] [ N ]; //strategia ertekosszeg
		temp = new int [ N ] [ N ]; //temporalis strategia
		m = new MatrixGenerator();
		for(int i = 0; i < N ; i++)
			for(int j = 0; j < N ; j++)
			{
				b[i][j] = 0;                // kezdoertek 0
			}
		a = m.randomMatrixGenerator(N);		
		
	}
	
	public void run()
	{	
		/*
		System.out.println("Kezdo strategiak");
		for (int i=0; i<N; i++)
		  { for (int j=0;j<N;j++) {
		    System.out.print( a[i][j]+" ");
		  } 
		  System.out.println();
		  }
		System.out.println();
		*/
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						// TODO Auto-generated method stub
						int k = 1; // lepesszam
						while (k <= 10 )
						{
						try {
							Thread.sleep(100);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						canvas.repaint();	
										
						System.out.println("Szimulacio repaint");
							for( int i = 0; i < N; i++)
								for( int j = 0; j < N; j++)
								{
									if( i == N-1 && j != N-1) //utolso sor esete
									{
										jobbra(a[i][j],a[i][j+1],i,j);
									}
									else if ( j == N-1 && i != N-1 ) //utolso oszlop esete
									{
										lefele(a[i][j],a[i+1][j],i,j);
									}
									else if ( i != N-1 && j != N-1) //utolso elemen kivul a tobbi
									{
										lefele(a[i][j],a[i+1][j],i,j);
										jobbra(a[i][j],a[i][j+1],i,j);
									}
								}
							System.out.println(k + "lepes");
							/*
							for (int i=0; i<N; i++)
							  { for (int j=0;j<N;j++) {
							    System.out.print( b[i][j]+" ");
							  } 
							  System.out.println();
							  }
							System.out.println();*/
							
							
						
						
						// mindegyik megnezi korulotte levo legnagyobb nyeremenyt s kivalasztja annak strategiajat
						
						//  1.......6.......2
						//  .................
						//  5.......9.......7
						//  .................
						//  4.......8.......3
						
						int max_index_i, max_index_j;
						
						for( int i = 0; i < N-1; i++)
							for( int j = 0; j < N-1; j++)
							{
							 max_index_i = i;
							 max_index_j = j;
							 if ( i == 0 && j ==0 ) // 1.eset
							 {
								 // also szomszed
								 if (b[max_index_i][max_index_j] < b[i+1][j])
								 {
									 max_index_i = i+1;
									 max_index_j = j;
								 }
								 // jobb szomszed
								 if (b[max_index_i][max_index_j] < b[i][j+1])
								 {
									 max_index_i = i;
									 max_index_j = j+1;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if ( i == 0 && j == N-1 ) //2.eset
							 {
								 // bal szomszed
								 if (b[max_index_i][max_index_j] < b[i][j-1])
								 {
									 max_index_i = i;
									 max_index_j = j-1;
								 }
								 // also szomszed
								 if (b[max_index_i][max_index_j] < b[i+1][j])
								 {
									 max_index_i = i+1;
									 max_index_j = j;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if  ( i == N-1 && j == N-1 ) //3.eset
							 {
								 // felso szomszed
								 if (b[max_index_i][max_index_j] < b[i-1][j])
								 {
									 max_index_i = i-1;
									 max_index_j = j;
								 }
								 // bal szomszed szomszed
								 if (b[max_index_i][max_index_j] < b[i][j-1])
								 {
									 max_index_i = i;
									 max_index_j = j-1;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if ( i == N-1 && j == 0) //4.eset
							 {
								 // felso szomszed
								 if (b[max_index_i][max_index_j] < b[i-1][j])
								 {
									 max_index_i = i-1;
									 max_index_j = j;
								 }
								 // jobb szomszed
								 if (b[max_index_i][max_index_j] < b[i][j+1])
								 {
									 max_index_i = i;
									 max_index_j = j+1;
								 }

								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if( i > 0 && i < N-1 && j == 0 ) //5.eset
							 {
								// felso szomszed
								 if (b[max_index_i][max_index_j] < b[i-1][j])
								 {
									 max_index_i = i-1;
									 max_index_j = j;
								 }
								// jobb szomszed
								 if (b[max_index_i][max_index_j] < b[i][j+1])
								 {
									 max_index_i = i;
									 max_index_j = j+1;
								 }
								// also szomszed
								 if (b[max_index_i][max_index_j] < b[i+1][j])
								 {
									 max_index_i = i+1;
									 max_index_j = j;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if( i == 0 && j > 0 && j< N-1 ) //6.eset
							 {
								// bal szomszed
								 if (b[max_index_i][max_index_j] < b[i][j-1])
								 {
									 max_index_i = i;
									 max_index_j = j-1;
								 }
								// also szomszed
								 if (b[max_index_i][max_index_j] < b[i+1][j])
								 {
									 max_index_i = i+1;
									 max_index_j = j;
								 }
								// jobb szomszed
								 if (b[max_index_i][max_index_j] < b[i][j+1])
								 {
									 max_index_i = i;
									 max_index_j = j+1;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if( i > 0 && i < N-1 && j == N-1 ) //7.eset
							 {
								// felso szomszed
								 if (b[max_index_i][max_index_j] < b[i-1][j])
								 {
									 max_index_i = i-1;
									 max_index_j = j;
								 }
								// bal szomszed szomszed
								 if (b[max_index_i][max_index_j] < b[i][j-1])
								 {
									 max_index_i = i;
									 max_index_j = j-1;
								 }
								// also szomszed
								 if (b[max_index_i][max_index_j] < b[i+1][j])
								 {
									 max_index_i = i+1;
									 max_index_j = j;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else if( i == N-1 && j > 0 && j < N-1 ) //8.eset
							 {
								// bal szomszed szomszed
								 if (b[max_index_i][max_index_j] < b[i][j-1])
								 {
									 max_index_i = i;
									 max_index_j = j-1;
								 }
								// felso szomszed
								 if (b[max_index_i][max_index_j] < b[i-1][j])
								 {
									 max_index_i = i-1;
									 max_index_j = j;
								 }
								// jobb szomszed
								 if (b[max_index_i][max_index_j] < b[i][j+1])
								 {
									 max_index_i = i;
									 max_index_j = j+1;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							 else //9.eset
							 {
								// felso szomszed
								 if (b[max_index_i][max_index_j] < b[i-1][j])
								 {
									 max_index_i = i-1;
									 max_index_j = j;
								 }
								// jobb szomszed
								 if(b[max_index_i][max_index_j] < b[i][j+1])
								 {
									 max_index_i = i;
									 max_index_j = j+1;
								 }
								// also szomszed
								 if (b[max_index_i][max_index_j] < b[i+1][j])
								 {
									 max_index_i = i+1;
									 max_index_j = j;
								 }
								// bal szomszed szomszed
								 if (b[max_index_i][max_index_j] < b[i][j-1])
								 {
									 max_index_i = i;
									 max_index_j = j-1;
								 }
								 
								 temp[i][j] = a[max_index_i][max_index_j];
							 }
							}
						
						// uj strategia kiiratas
						/*
						System.out.println("Uj strategia");
						for (int i=0; i<N; i++)
						  { for (int j=0;j<N;j++) {
						    System.out.print( temp[i][j]+" ");
						  } 
						  System.out.println();
						  }
						System.out.println();*/
						
						
						// b matrix lenullazas az uj lepes miatt
						for (int i=0; i<N; i++)
						   for (int j=0;j<N;j++) {
							  a[i][j]=temp[i][j];
							  b[i][j] = 0;
						  }		
						k++;
						}							
		
	}
	
	private void jobbra(int strategia1,int strategia2, int i, int j)
	{
		if ( strategia1 == 1 && strategia2 == 1) //Cooperate - Cooperate
		{
			b[i][j] = b[i][j] + R;
			b[i][j+1] = b[i][j+1] + R;
		}
		
		if ( strategia1 == 1 && strategia2 == 0) //Cooperate - Defect
		{
			b[i][j] = b[i][j] + S;
			b[i][j+1] = b[i][j+1] + T;
		}		
		
		if ( strategia1 == 0 && strategia2 == 1)//Defect - Coopereate
		{
			b[i][j] = b[i][j] + T;
			b[i][j+1] = b[i][j+1] + S;
		}
		
		if ( strategia1 == 0 && strategia2 == 0)//Defect - Defect
		{
			b[i][j] = b[i][j] + P;
			b[i][j+1] = b[i][j+1] + P;
		}
	}
	
	private void lefele(int strategia1,int strategia2,int i, int j)
	{
		if ( strategia1 == 1 && strategia2 == 1) //Cooperate - Cooperate
		{
			b[i][j] = b[i][j] + R;
			b[i+1][j] = b[i+1][j] + R;
		}
		
		if ( strategia1 == 1 && strategia2 == 0) //Cooperate - Defect
		{
			b[i][j] = b[i][j] + S;
			b[i+1][j] = b[i+1][j] + T;
		}		
		
		if ( strategia1 == 0 && strategia2 == 1)//Defect - Coopereate
		{
			b[i][j] = b[i][j] + T;
			b[i+1][j] = b[i+1][j] + S;
		}
		
		if ( strategia1 == 0 && strategia2 == 0)//Defect - Defect
		{
			b[i][j] = b[i][j] + P;
			b[i+1][j] = b[i+1][j] + P;
		}
	}
}

// Lepesszam, arany (C/osssz), c?,
// kep kimentess
